/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scene.ui.template;

import com.bytedance.scene.ui.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

/**
 * 描述
 *
 * @since 2021-06-02
 */
public class MyMenuProvider_two extends BaseItemProvider {
    private static final int FFF5F4 = 0xFFF5F4F4;
    private static final int FFFFFF = 0xFFFFFFFF;
    private int selectedPosition = 0;

    private final Context mContext;
    private final List<String> listData;

    /**
     * MyMenuProvider_two
     *
     * @param mContext mContext
     * @param listData listData
     */
    public MyMenuProvider_two(Context mContext, List<String> listData) {
        this.mContext = mContext;
        this.listData = listData;
    }

    /**
     * setSelectedPosition
     *
     * @param selectedPosition selectedPosition
     */
    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i1) {
        return listData.get(i1);
    }

    @Override
    public long getItemId(int i1) {
        return i1;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ComponentHolder holder;
        String data = listData.get(position);
        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_navigation_menu,
                    componentContainer, false);
            holder = new ComponentHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (ComponentHolder) cpt.getTag();
        }

        holder.textContent.setText(data);
        if (position == selectedPosition) {
            holder.textContent.setTextColor(new Color(Color.getIntColor("#3F51B5")));
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(FFF5F4));
            holder.title_content.setBackground(shapeElement);
        } else {
            holder.textContent.setTextColor(new Color(Color.getIntColor("#202020")));
            ShapeElement shapeElement2 = new ShapeElement();
            shapeElement2.setRgbColor(RgbColor.fromArgbInt(FFFFFF));
            holder.title_content.setBackground(shapeElement2);
        }

        return cpt;
    }

    /**
     * 描述
     *
     * @since 2021-06-02
     */
    public static class ComponentHolder {
        Text textContent;
        DirectionalLayout title_content;

        /**
         * ComponentHolder
         *
         * @param component component
         */
        public ComponentHolder(Component component) {
            textContent = (Text) component.findComponentById(ResourceTable.Id_text_item_navigation_menu_content);
            title_content = (DirectionalLayout) component.findComponentById(ResourceTable.Id_title_content);
        }
    }
}